﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace Trade.Journal
{
    public class Opportunity:IFormattable
    {

        public BasicType BasicType { get; set; }

        public CategoryType[] CategoryType { get; set; }

        public bool IsWellFormed
        {
            get
            {
                switch (BasicType)
                {
                    case BasicType.Normal: 
                    case BasicType.Overweight:
                    case BasicType.Balance:
                        return  CategoryType != null && CategoryType.Length > 0; 
                    case BasicType.Resonate:
                        return CategoryType != null && CategoryType.Length > 1; 
                    default:
                        return false;
                }
            }
        }

        public Opportunity() { }


        public static Opportunity Parse(string s)
        {
            if (string.IsNullOrWhiteSpace(s))
                return null;

            var part = s.Split('|');
            if (part.Length != 2)
                return null;

            if (!BasicType.TryParse(part[0], out BasicType bt))
                return null;

            Opportunity o = new Opportunity();
            o.BasicType = bt;

            var ct = part[1].Split('#');
            List<CategoryType> list = new List<CategoryType>();
            foreach (var item in ct)
            {
                if (Enum.TryParse(item, out CategoryType c))
                    list.Add(c);
            }
            o.CategoryType = list.ToArray();
            return o;
        }

        public override string ToString()
        {
            if (CategoryType == null || CategoryType.Length == 0)
                return BasicType.ToString();
            else
                return $"{BasicType}|{string.Join("#", CategoryType)}";
        }

        public string ToString(string format)
        {
            if (CategoryType == null || CategoryType.Length == 0)
                return string.Empty;

            if (format == "D")
                return $"{GetDescription(BasicType)}|{string.Join("#", CategoryType.Select(x => GetDescription(x)))}";
            else if (format == "N")
                return $"{(int)BasicType}|{string.Join("#", CategoryType.Select(x => (int)x))}";
            else
                return string.Empty;
        }

        private string GetDescription(Enum e)
        {

            System.Reflection.FieldInfo field = e.GetType().GetField(e.ToString());
            object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (objs.Length == 0)
                return string.Empty;
            DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
            return descriptionAttribute.Description;

        }

        public string ToString(string format, IFormatProvider formatProvider)
        { 
            return ToString(format);
        }
    }
}
